<?php
include "Path.php";
include "Image.php";
include "Connector.php";

final class Utils
{
    static public function print($code, $data)
    {
        $temp["code"] = $code;
        $temp["data"] = $data;
        echo json_encode($temp);
    }

    static public function upload($files, $dir)
    {
        $format = [];
        foreach ($files as $key => $value) {
            $temp = [];
            foreach ($value as $k => $v) {
                if (is_array($v)) {
                    for ($i = 0; $i < count($v); $i++) {
                        $temp[$i][$k] = $v[$i];
                    }
                } else {
                    $temp[0][$k] = $v;
                }
            }
            foreach ($temp as $k => $v) {
                $format[] = $v;
            }
        }
        //
        if (count($format) > 0) {
            $temp = [];
            foreach ($format as $k => $v) {
                $src = Path::revise("$dir/" . $v["name"]);
                if (Path::directory($src)) {
                    if (move_uploaded_file($v["tmp_name"], $src)) {
                        $temp[$src] = Path::fileMime($v["type"]);
                    }
                }
            }
            return $temp;
        }
        return false;
    }
}